package org.jdesktop.databuffer;

import java.io.Serializable;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class BaseObject implements Serializable, Cloneable {
	
	  private static final long serialVersionUID = 440646353502465128L;
	  private Map attr;

	  public String toString()
	  {
	    return new ReflectionToStringBuilder(this).toString();
	  }

	  public boolean equals(Object obj)
	  {
	    return EqualsBuilder.reflectionEquals(this, obj);
	  }

	  public int hashCode()
	  {
	    return HashCodeBuilder.reflectionHashCode(this);
	  }

	  public Object clone()
	  {
	    try
	    {
	      return BeanUtils.cloneBean(this);
	    } catch (Exception e) {	      
	    }return null;
	  }

	  public Object deepClone()
	  {
	    try
	    {
	      BaseObject newObj = (BaseObject)clone();
	      if (this.attr != null)
	        newObj.setAttr((HashMap)((HashMap)this.attr).clone());
	      return newObj;
	    } catch (Exception e) {
	    }return null;
	  }

	  public Map getAttr()
	  {
	    return this.attr;
	  }

	  public void setAttr(Map attr)
	  {
	    this.attr = attr;
	  }

	  public Object get(String key)
	  {
	    return this.attr == null ? null : this.attr.get(key);
	  }

	  public String getString(String key)
	  {
	    return get(key) == null ? null : String.valueOf(get(key));
	  }

	  public int getInt(String key)
	  {
	    try
	    {
	      return Integer.parseInt(String.valueOf(get(key))); } catch (Exception e) {
	    }
	    return 0;
	  }

	  public String getAttrKeys()
	  {
	    if (this.attr == null) {
	      return "";
	    }
	    StringBuffer sb = new StringBuffer();
	    for (Iterator i = this.attr.keySet().iterator(); i.hasNext(); ) {
	      sb.append((String)i.next() + ",");
	    }
	    return StringUtils.substringBeforeLast(sb.toString(), ",");
	  }

	  public void set(String key, Object value)
	  {
	    if (this.attr == null) {
	      this.attr = new HashMap();
	    }
	    this.attr.put(key, value);
	  }
}
